﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public static class AmbulanceDataAccess
    {

        static MedicalCenterEntities entities = MedicalCenterEntities.Instance;

        public static string AmbulanceRegister(Ambulance newAmbulance)
        {
            try
            {
                if (newAmbulance != null)
                {
                    entities.Ambulances.Add(newAmbulance);
                    entities.SaveChanges();
                }
            }
            catch (NullReferenceException)
            {
                return "Error al registrar la ambulancia";
            }
            return "La ambulancia ha sido creada con exito";
        }

        public static ICollection<Ambulance> AmbulanceSearch()
        {            
                var ambulanceList = from ambulance in entities.Ambulances
                                    where ambulance.Deleted != "Yes"
                                    select ambulance;
                return ambulanceList.ToList();                   
        }

        public static string AmbulanceModify(string ambulanceId, Ambulance ambulanceModified)
        {           
                if (ambulanceId != null && ambulanceModified != null)
                {
                    Ambulance ambulance = entities.Ambulances.First(m => m.AmbulanceId == ambulanceId);
                    ambulance.Model = ambulanceModified.Model;
                    ambulance.Year = ambulanceModified.Year;
                    ambulance.PurchaseYear = ambulanceModified.PurchaseYear;
                    entities.SaveChanges();
                    return "Cambios guardados con exito";
                }
                else {
                    return "Hubo un problema al guardar los cambios";
                }
        }

        public static bool AmbulanceExists(string ambulanceId)
        {
            Ambulance ambulance = entities.Ambulances.Find(ambulanceId);
            if(ambulance != null && ambulance.Deleted == "No")
                return true;
            else
            return false;
        }

        public static Ambulance ObtainAmbulance(string ambulanceId)
        {
            try 
            {
                Ambulance ambulance = entities.Ambulances.First(m => m.AmbulanceId == ambulanceId);
                return ambulance;
            }
            catch(NullReferenceException) 
            {
                return null;
            }
        }

        public static string DeleteAmbulance(string ambulanceId)
        {
            if (ambulanceId != null)
            {
                Ambulance ambulance = entities.Ambulances.First(m => m.AmbulanceId == ambulanceId);
                ambulance.Deleted = "Yes";
                entities.SaveChanges();
                return "Ambulancia eliminada con exito";
            }
            else
                return "Error al eliminar ambulancia";
        }

        public static string AssignAmbulance(string ambulanceId)
        {
            if (ambulanceId != null)
            {
                Ambulance ambulance = entities.Ambulances.First(m => m.AmbulanceId == ambulanceId);
                ambulance.State = "Ocupada";
                entities.SaveChanges();
                return "Ambulancia asignada con exito";
            }
            else
                return "Error al asignar ambulancia";
        }

        public static string UnassignAmbulance(string ambulanceId)
        {
            if (ambulanceId != null)
            {
                Ambulance ambulance = entities.Ambulances.First(m => m.AmbulanceId == ambulanceId);
                ambulance.State = "Libre";
                entities.SaveChanges();
                return "Ambulancia liberada con exito";
            }
            else
                return "Error al liberar ambulancia";
        }

        public static ICollection<Ambulance> AmbulanceAssignedSearch()
        {
            var ambulanceList = from ambulance in entities.Ambulances
                                where ambulance.Deleted != "Libre"
                                select ambulance;
            return ambulanceList.ToList();
        }

        public static ICollection<Ambulance> AmbulanceFreeSearch()
        {
            var ambulanceList = from ambulance in entities.Ambulances
                                where ambulance.Deleted != "Ocupada"
                                select ambulance;
            return ambulanceList.ToList();
        }

        /*public static string AmbulanceCaseRegister(AmbulanceCase newAmbulanceCase)
        {
            try
            {
                if (newAmbulanceCase != null)
                {
                    entities.AmbulanceCases.Add(newAmbulanceCase);
                    entities.SaveChanges();
                }
            }
            catch (NullReferenceException)
            {
                return "Error al registrar el caso";
            }
            return "El caso ha sido creado con exito";
        }

        public static string UNAssignAmbulance(string ambulanceId)
        {
            ambulanceId += "";
            throw new NotImplementedException();
        }*/
    }
}
